package com.mida.bms.report.controller;


import com.mida.bms.report.entity.DocStructTemplateInfo;
import com.mida.bms.report.service.DocStructTemplateInfoService;
import com.mida.bms.report.vo.DocStructTemplateAuthBatchVo;
import com.mida.bms.report.vo.DocStructTemplateInfoVo;
import com.mida.bms.report.vo.PageResultVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import com.mida.tool.common.validator.InsertValidator;
import com.mida.tool.common.validator.UpdateValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;


/**
 * @author ：dj
 * @description：模版基本信息
 */
@RestController
@RequestMapping("/docStructTemplateInfo")
@CrossOrigin
@Slf4j
public class DocStructTemplateInfoController {

    @Autowired
    private DocStructTemplateInfoService docStructTemplateInfoService;

    /**
     * 添加模版基本信息
     * @param docStructTemplateInfoVo
     * @return
     */
    @PostMapping(value = "/addDocStructTemplateInfo",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult addDocStructTemplateInfo(
            @Validated(InsertValidator.class)
            @RequestBody DocStructTemplateInfoVo docStructTemplateInfoVo) {

        ActionResult result = new ActionResult();
        try {
             result = docStructTemplateInfoService.addDocStructTemplateInfo(docStructTemplateInfoVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("添加模版基本信息失败");
            return result;
        }
        return result;
    }

    /**
     * 修改模版数据
     *
     * @param docStructTemplateInfoVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/updateDocStructTemplateInfo",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult updateDocStructTemplateInfo(
            @Validated(UpdateValidator.class)
            @RequestBody DocStructTemplateInfoVo docStructTemplateInfoVo) {
        ActionResult result = new ActionResult();
        try {
            result = docStructTemplateInfoService.updateDocStructTemplateInfo(docStructTemplateInfoVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("修改模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 删除模版数据
     *
     * @param stTemplateInfoSoid 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/deleteDocStructTemplateInfo",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult deleteDocStructTemplateInfo(@RequestBody Long stTemplateInfoSoid) {
        ActionResult result = new ActionResult();
        try {
            result = docStructTemplateInfoService.deleteDocStructTemplateInfo(stTemplateInfoSoid);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("删除模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 查询模板分页列表处理
     *
     * @param docStructTemplateInfoVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getDocStructTemplateInfoPage",produces = {"application/json;charset=UTF-8"})
    public ListResult<PageResultVo<DocStructTemplateInfo>> getDocStructTemplateInfoPage(
            @RequestBody DocStructTemplateInfoVo docStructTemplateInfoVo) {
        ListResult result = new ListResult();
        try {
            PageResultVo<DocStructTemplateInfo> data =
                    docStructTemplateInfoService.getDocStructTemplateInfoPage(docStructTemplateInfoVo);
            buildListSuccess(result, data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("查询模板分页列表处理失败");
            return result;
        }
        return result;
    }

    /**
     * 根据模板soid查询模版数据
     *
     * @param stTemplateInfoSoid 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getDocStructTemplateInfo",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult getDocStructTemplateInfo(@RequestBody Long stTemplateInfoSoid) {
        ListResult result = new ListResult();
        try {
            result = docStructTemplateInfoService.getDocStructTemplateInfo(stTemplateInfoSoid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("根据模板soid查询模版数据失败");
            return result;
        }
        return result;
    }


    /**
     * 查询可授权模板列表分页列表处理
     *
     * @param docStructTemplateAuthBatchVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getAuthSpaceDocStructTemplateInfoPage",produces = {"application/json;charset=UTF-8"})
    public ListResult<PageResultVo<DocStructTemplateInfo>> getAuthSpaceDocStructTemplateInfoPage(
            @RequestBody DocStructTemplateAuthBatchVo docStructTemplateAuthBatchVo) {
        ListResult result = new ListResult();
        try {
            PageResultVo<DocStructTemplateInfo> data =
                    docStructTemplateInfoService.getAuthSpaceDocStructTemplateInfoPage(docStructTemplateAuthBatchVo);
            buildListSuccess(result, data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("查询可授权模板列表分页列表处理失败,请联系管理员");
            return result;
        }
        return result;
    }

    /**
     * 查询可授权模板列表处理
     *
     * @param docStructTemplateAuthBatchVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getAuthSpaceDocStructTemplateInfoList",produces = {"application/json;charset=UTF-8"})
    public ListResult<List<DocStructTemplateInfo>> getAuthSpaceDocStructTemplateInfoList(
            @RequestBody DocStructTemplateAuthBatchVo docStructTemplateAuthBatchVo) {
        ListResult result = new ListResult();
        try {
            List<DocStructTemplateInfo> data =
                    docStructTemplateInfoService.getAuthSpaceDocStructTemplateInfoList(docStructTemplateAuthBatchVo);
            buildListSuccess(result, data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("查询可授权模板列表分页列表处理失败,请联系管理员");
            return result;
        }
        return result;
    }
}
